package com.lzx.adx.report.support.strategy.v3.channel;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ProductConfigDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.support.strategy.v3.media.AbstractChannel;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@ProductType(ProductChannelConstant.TBUNION)
public class TbUnionReport extends AbstractProductReport {


    @Override
    public String execute(ReportDTO reportDTO, OcpxAccountDTO ocpxAccountDTO, ProductConfigDTO productConfigDTO) {


        Map<String,Object> prams=new HashMap<>();
        prams.put("channel",ocpxAccountDTO.getChannelId());
        prams.put("tbkId",ocpxAccountDTO.getTagId());
        prams.put("taskId",ocpxAccountDTO.getTaskId());
        prams.put("sourcePack",ocpxAccountDTO.getPlanId());
        prams.put("adid",reportDTO.getCampaignId());
        prams.put("cid",reportDTO.getContentId());
        prams.put("adGroupId",reportDTO.getAdgroupId());
        prams.put("imeiMd5",reportDTO.getImei());
        prams.put("oaid",reportDTO.getOaid());
        prams.put("oaidMd5",reportDTO.getOaidSum());
        prams.put("idfa",reportDTO.getIdfa());
        prams.put("idfaMd5",reportDTO.getIdfaSum());
        prams.put("caid",reportDTO.getCaid());
        prams.put("caidMd5",reportDTO.getCaidSum());
        prams.put("actionTime",System.currentTimeMillis()/1000);

        String callbackUrl=String.format(productConfigDTO.getCallbackUrl(),reportDTO.getBizRequestId());
        String urlParams= UrlUtil.covertParamsOrderByAsc(prams)+"&callbackUrl="+ URLEncoderDecoderTool.encodeURL(callbackUrl);
        String finalUrl = ocpxAccountDTO.getReportUrl() + "&" + urlParams;

        log.info("账号为{},请求淘宝联盟的完整地址为 {}",ocpxAccountDTO.getId(),finalUrl);
        //2.2调用饿了嘛，上报接口
        String body = HttpUtil.createGet(finalUrl).execute().body();
        log.info("账号为{} 请求淘宝联盟，响应的结果为{}",ocpxAccountDTO.getId(),  body);

        ocpxAccountDTO.setFinalURL(finalUrl);
        return body;
    }
}
